﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NEMproject
{
    class DataRequest
    {
        static SQLiteDatabase dg = new SQLiteDatabase();
        private static int YearHorizon = -1;
        private static int BaseFuelPrice = -1;
        private static int[] PriceThr;
        private static double[] VarPr;
        private static double[] FixedPr;
        private static double[] CFP;
        private static double coef_fuel = -1;
        private static double VAT_rate = -1;
        private static int startingY = -1;
        //private static int[] Hopt;


        public static DataTable getCities()
        {
            return dg.GetDataTable("select City from MonthlyIrradiation;");
        }

        /// <summary>
        /// Return the YearHorizon value.
        /// </summary>
        /// <returns></returns>
        public static int getYearHorizon()
        {
            if (YearHorizon != -1)
            {
                return YearHorizon;
            }
            else
            {
                YearHorizon = Int32.Parse(dg.ExecuteScalar("select YearHorizon from UtilityValues;"));
                return YearHorizon;
            }

        }

        /// <summary>
        /// Returns the BasicFuelPrice.
        /// </summary>
        /// <returns></returns>
        public static int getBaseFuelPrice()
        {
            if (BaseFuelPrice == -1)
            {
                BaseFuelPrice = Int32.Parse(dg.ExecuteScalar("select BaseFuelPrice from utilityValues;"));
                return BaseFuelPrice;
            }
            else
            {
                return BaseFuelPrice;
            }
        }

        /// <summary>
        /// Returns the price values that serve as thresholds. 
        /// </summary>
        /// <returns></returns>
        public static int[] getPriceThresholds()
        {
            if (PriceThr == null)
            {
                String query = "select Value from PriceThresholds;";
                DataTable result = dg.GetDataTable(query);
                PriceThr = new int[result.Rows.Count];
                int i = 0;
                foreach (DataRow r in result.Rows)
                {
                    PriceThr[i++] = Int32.Parse(r["Value"].ToString());
                }

                return PriceThr;
            }
            else
            {
                return PriceThr;
            }
        }

        /// <summary>
        /// Returns the tariff for each charge scale based on the price thresholds.
        /// </summary>
        /// <returns></returns>
        public static double[] getVariablePrices()
        {
            if (VarPr == null)
            {
                String query = "select Tariff from Prices;";
                DataTable result = dg.GetDataTable(query);
                VarPr = new double[result.Rows.Count];
                int i = 0;
                foreach (DataRow r in result.Rows)
                {
                    VarPr[i++] = double.Parse(r["Tariff"].ToString());
                }

                return VarPr;
            }
            else
            {
                return VarPr;
            }

        }

        /// <summary>
        /// Returns the fixed price for each charge scale based on the price thresholds.
        /// </summary>
        /// <returns></returns>
        public static double[] getFixedPrices()
        {
            if (FixedPr == null)
            {
                String query = "select FixedPrice from Prices;";
                DataTable result = dg.GetDataTable(query);
                FixedPr = new double[result.Rows.Count];
                int i = 0;
                foreach (DataRow r in result.Rows)
                {
                    FixedPr[i++] = double.Parse(r["FixedPrice"].ToString());
                }

                return FixedPr;
            }
            else
            {
                return FixedPr;
            }


        }

        /// <summary>
        /// Returns the Current Fuel Prices for the next YearHorizon based on the fiven starting year.
        /// </summary>
        /// <param name="startingYear">The starting year based on which the Current Fuel Prices for the YearHorizon will be returned.</param>
        /// <returns></returns>
        public static double[] getCFP(int startingYear)
        {
            if (CFP == null || startingY == startingYear)
            {
                if (YearHorizon == -1)
                {
                    YearHorizon = getYearHorizon();
                }
                String query = "select FuelPrice from CurrentFuelPrices where Year>=" + startingYear + " and Year<" + (YearHorizon + startingYear) + ";";
                DataTable result = dg.GetDataTable(query);
                CFP = new double[result.Rows.Count];
                int i = 0;
                foreach (DataRow r in result.Rows)
                {
                    CFP[i++] = double.Parse(r["FuelPrice"].ToString());
                }
                startingY = startingYear;
                return CFP;

            }
            else
            {
                return CFP;
            }




        }

        /// <summary>
        /// Returs the Fuel Coefficient.
        /// </summary>
        /// <returns></returns>
        public static double getFuelCoefficient()
        {
            if (coef_fuel != -1)
            {
                return coef_fuel;
            }
            else
            {
                coef_fuel = double.Parse(dg.ExecuteScalar("select FuelCoefficient from UtilityValues;"));
                return coef_fuel;
            }

        }

        /// <summary>
        /// Returns the VAT Rate.
        /// </summary>
        /// <returns></returns>
        public static double getVATRate()
        {
            if (VAT_rate != -1)
            {
                return VAT_rate;
            }
            else
            {
                VAT_rate = double.Parse(dg.ExecuteScalar("select VATRate from UtilityValues;"));
                return VAT_rate;
            }

        }

        /// <summary>
        /// Returns Hopt given a city.
        /// </summary>
        /// <returns></returns>
        public static int[] getHopt(String city)
        {              
            String query = "select Hopt from Irradiation where City ='" + city.Trim() + "';";
            DataTable result = dg.GetDataTable(query);
            int[] Hopt = new int[result.Rows.Count];
            
            int i = 0;
            foreach (DataRow r in result.Rows)
            {
                Hopt[i++] = Int32.Parse(r["Hopt"].ToString());
            }

            return Hopt;
        }
    }
}
